<?php

namespace app\admin\controller;
use think\Db;
use app\admin\model\ShopStorestaff as ShopStorestaffModel;

class ShopStorestaff extends Common
{
    //广告列表
    public function lst()
    {
        $shopId           = session('shop_id');
        $where['a.deleted'] = 0;
        $where['a.shop_id'] = $shopId;
        $list = Db::name('shop_storestaff')->alias('a')
            ->field('a.id,a.name,a.contact,a.addtime,a.status,m.user_name,ss.name as staff_name')
            ->join('member m','m.id = a.user_id','LEFT')
            ->join('shop_store ss','ss.id = a.ss_id','LEFT')
            ->where($where)
            ->order('a.id asc')
            ->paginate(10);
        $page = $list->render();

        if(input('page')){
            $pnum = input('page');
        }else{
            $pnum = 1;
        }
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->assign('pnum', $pnum);
        if (request()->isAjax()) {
            return $this->fetch('ajaxpage');
        } else {
            return $this->fetch();
        }
    }


    public function teng_xun_map(){
        return $this->fetch();
    }


    public function checkAdname()
    {
        if (request()->isAjax()) {
            $name = input('post.name');
            if(isset($name) && !empty($name)){
                $ad_name = Db::name('shop_store')->where('name',$name)->find();
                echo $ad_name ? 'false': 'true';die();
            }
            $phone = input('post.phone');
            if(isset($phone) && !empty($phone)){
                $ad_name = Db::name('member')->where('phone',$phone)->find();
                echo $ad_name ? 'true': 'false';die();
            }
        }
    }

    //获取列表
    public function poslist()
    {
        $id = input('cate_id');
        $cate_name = Db::name('ad_cate')->where('id', $id)->value('cate_name');
        $adCateList = Db::name('ad_cate')->field('id,cate_name,width,height')->order('id asc')->select();
        $list = Db::name('ad')->alias('a')->field('a.id,a.ad_name,a.is_on,b.cate_name')->join('sp_ad_cate b', 'a.cate_id = b.id', 'LEFT')->where('a.cate_id', $id)->order('a.id desc')->paginate(25);
        $page = $list->render();
        if (input('page')) {
            $pnum = input('page');
        } else {
            $pnum = 1;
        }

        $this->assign('cate_id', $id);
        $this->assign('cate_name', $cate_name);
        $this->assign('pnum', $pnum);
        $this->assign('adCateList', $adCateList);
        $this->assign('list', $list);
        $this->assign('page', $page);
        if (request()->isAjax()) {
            return $this->fetch('ajaxpage');
        } else {
            return $this->fetch('lst');
        }
    }

    //修改状态
    public function gaibian()
    {
        $id = input('post.id');
        $name = input('post.name');
        $value = input('post.value');
        $data[$name] = $value;
        $data['id'] = $id;
        $ads = Db::name('shop_storestaff')->where('id', $data['id'])->find();
        if ($ads) {
            $count = Db::name('shop_storestaff')->update($data);
            if ($count > 0) {
                if ($value == 1) {
                    // Db::name('ad')->where('cate_id',$ads['cate_id'])->where('id','neq',$ads['id'])->update(array('is_on'=>0));
                    ys_admin_logs('启用员工', 'shop_storestoff', $id);
                } elseif ($value == 0) {
                    ys_admin_logs('禁用员工', 'shop_storestoff', $id);
                }
                $result = 1;
            } else {
                $result = 0;
            }
        } else {
            $result = 0;
        }
        return $result;
    }


    //添加门店
    public function add()
    {
        $shopId           = session('shop_id');
        if (request()->isAjax()) {
            $data = input('post.');
            $member_user = Db::name("member")->where("phone",$data['phone'])->find();
            if(empty($member_user)){
                $value = array('status'=>0,'mess'=>'没有找到用户');
            }else{
                $shop_staff = Db::name("shop_storestaff")->where("ss_id",$data['ss_id'])->where("user_id",$member_user['id'])->where("deleted",0)->find();
                if($shop_staff){
                    $value = array('status'=>0,'mess'=>'当前用户已经是该门店的员工');
                }else{
                    $result = $this->validate($data,'ShopStorestaff');
                    if(true !== $result){
                        $value = array('status'=>0,'mess'=>$result);
                    }else{
                        $data['user_id'] = $member_user['id'];
                        $data['addtime'] = time();
                        $data['updatetime'] = time();
                        $data['shop_id'] = $shopId;
                        $data['phone'] = $member_user['phone'];
                        $shopStoreModel = new ShopStorestaffModel();
                        $shopStoreModel->data($data);
                        $lastId = $shopStoreModel->allowField(true)->save();
                        if($lastId){
//                            ys_admin_logs('新增店员','shop_storestaff',$lastId->id);
                            $value = array('status'=>1, 'mess'=>'增加成功');
                        }else{
                            $value = array('status'=>0, 'mess'=>'增加失败');
                        }
                    }
                }
            }

            return json($value);
        } else {
            $shop_store = Db::name('shop_store')->where("shop_id",$shopId)->where('deleted',0)->where('status',1)->field('id,name')->select();
            $this->assign('shop_store',$shop_store);

            return $this->fetch();
        }
    }

    //编辑门店
    public function edit()
    {
        if (request()->isAjax()) {
            if (input('post.id')) {
                $data = input('post.');
                $result = $this->validate($data, 'ShopStorestaff');
                if (true !== $result) {
                    datamsg(0,$result);
                }
                $member_user = Db::name("member")->where("phone",$data['phone'])->find();
                if(empty($member_user)){
                    datamsg(0, '没有找到用户');
                }
                $where['ss_id'] = $data['ss_id'];
                $where['user_id'] = $member_user['id'];
                $where['id'] = ['neq',$data['id']];
                $where['deleted'] = 0;
                $shop_staff = Db::name("shop_storestaff")->where($where)->find();
                if($shop_staff){
                    datamsg(0, '当前用户已经是该门店的员工');
                }
                $result = $this->validate($data,'ShopStorestaff');
                if(true !== $result){
                    datamsg(0, $result);
                }
                $data['user_id'] = $member_user['id'];
                $data['addtime'] = time();
                $data['updatetime'] = time();
                $data['phone'] = $member_user['phone'];
                $shopStorestaffModel = new ShopStorestaffModel();
                $edit = $shopStorestaffModel->where('id',$data['id'])->update($data);
                if($edit == false){
                    datamsg(0, '编辑失败');
                }
                datamsg(1, '编辑成功');
            } else {
                datamsg(0, '缺少参数');
            }
        } else {
            if (input('id')) {
                $id = input('id');
                $info = Db::name('shop_storestaff')->find($id);
                if ($info) {
                    $shopId           = session('shop_id');
                    $shop_store = Db::name('shop_store')->where("shop_id",$shopId)->where('deleted',0)->where('status',1)->field('id,name')->select();
                    $this->assign('shop_store',$shop_store);
                    $this->assign('info',$info);
                    return $this->fetch();
                } else {
                    $this->error('找不到相关信息');
                }
            } else {
                $this->error('缺少参数');
            }
        }
    }


    //删除门店
    public function delete()
    {
        if (input('post.id')) {
            $id = array_filter(explode(',', input('post.id')));
        } else {
            $id = input('id');
        }

        if (!empty($id)) {
            if (is_array($id)) {
                $delId = implode(',', $id);
                $arr2 = Db::name('shop_storestaff')->where('id', 'in', $delId)->update(['deleted'=>1]);
            } else {
                $arr2 = Db::name('shop_storestaff')->where('id', $id)->update(['deleted'=>1]);
            }
            if ($arr2) {
                $value = array('status' => 1, 'mess' => '删除成功');
            } else {
                $value = array('status' => 0, 'mess' => '删除失败');
            }
        } else {
            $value = array('status' => 0, 'mess' => '请选择删除项');
        }
        return json($value);
    }


    //搜索门店
    public function search()
    {
        $shopId           = session('shop_id');
        if (input('post.keyword')) {
            cookie('name', input('post.keyword'), 3600);
        }
        if (cookie('name')) {
            $where['a.name'] = array('like', '%' . cookie('name') . '%');
        }
        $where['a.shop_id'] = ["=",$shopId];

        $list = Db::name('shop_storestaff')
            ->alias('a')
            ->field('a.*')
            ->where($where)
            ->order('a.id desc')
            ->paginate(25);
        $page = $list->render();
        if (input('page')) {
            $pnum = input('page');
        } else {
            $pnum = 1;
        }
        if (cookie('name')) {
            $this->assign('name', cookie('name'));
        }
        $this->assign('pnum', $pnum);
        $this->assign('list', $list);// 赋值数据集
        $this->assign('page', $page);// 赋值分页输出
        if (request()->isAjax()) {
            return $this->fetch('ajaxpage');
        } else {
            return $this->fetch('lst');
        }
    }
}